package com.zw.yzk.kabu.presenter;

import android.support.v7.app.AppCompatActivity;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.base.BaseSubscriber;
import com.zw.yzk.kabu.data.network.entity.CosplayListEntity;
import com.zw.yzk.kabu.data.network.entity.FameListEntity;
import com.zw.yzk.kabu.data.network.task.CollectTask;
import com.zw.yzk.kabu.data.network.task.CosplayListTask;
import com.zw.yzk.kabu.data.network.task.FameListTask;
import com.zw.yzk.kabu.data.network.task.PraiseTask;
import com.zw.yzk.kabu.domain.repository.UseCase;
import com.zw.yzk.kabu.mapper.CosplayListMapper;
import com.zw.yzk.kabu.mapper.FameListMapper;
import com.zw.yzk.kabu.view.view.DataListView;

import javax.inject.Inject;
import javax.inject.Named;

/**
 * Created by zhanwei on 2017/1/4.
 */

public class DataListPresenter implements Presenter {

    private UseCase getCosplayList;
    private UseCase getFameList;
    private UseCase collectCosplay;
    private UseCase praiseCosplay;
    private AppCompatActivity activity;
    private CosplayListMapper cosMapper;
    private FameListMapper fameMapper;
    private DataListView dataListView;

    @Inject
    public DataListPresenter(@Named("cosplayUseCase") UseCase getCosplayList,
                             @Named("fameUseCase") UseCase getFameList,
                             @Named("collectUseCase") UseCase collectCosplay,
                             @Named("praiseUseCase") UseCase praiseCosplay,
                             AppCompatActivity appCompatActivity,
                             CosplayListMapper cosMapper,
                             FameListMapper fameMapper) {
        this.getCosplayList = getCosplayList;
        this.getFameList = getFameList;
        this.collectCosplay = collectCosplay;
        this.praiseCosplay = praiseCosplay;
        this.activity = appCompatActivity;
        this.cosMapper = cosMapper;
        this.fameMapper = fameMapper;
    }

    public void setView(DataListView view) {
        this.dataListView = view;
    }

    public void getCosplayList(int existCount, boolean getLocal, int type, int lastId) {
        getCosplayList.execute(new GetCosplayListSubscriber(activity), new CosplayListTask(existCount, getLocal, type, lastId));
    }

    public void setGetFameList(int existCount, boolean getLocal) {
        getFameList.execute(new GetFameListSubscriber(activity), new FameListTask(existCount, getLocal));
    }

    public void praise(int cosplayId, boolean praise) {
        praiseCosplay.execute(new PraiseCosplaySubscriber(activity), new PraiseTask(cosplayId, praise ? 1 : 0));
    }

    public void collect(int cosplayId, boolean collect) {
        collectCosplay.execute(new CollectCosplaySubscriber(activity), new CollectTask(cosplayId, collect ? 1 : 0));
    }

    @Override
    public void resume() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void destroy() {
        getCosplayList.unSubscribe();
        getFameList.unSubscribe();
        collectCosplay.unSubscribe();
        praiseCosplay.unSubscribe();
    }

    private class GetCosplayListSubscriber extends BaseSubscriber<CosplayListEntity> {

        public GetCosplayListSubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public void onNext(CosplayListEntity cosplayListEntity) {
            super.onNext(cosplayListEntity);
            dataListView.getCosplayListSuccess(cosMapper.transfer(cosplayListEntity));
        }

        @Override
        public void onCompleted() {
            super.onCompleted();
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            dataListView.getCosplayListFail();
        }

        @Override
        public String addErrorMessage() {
            return activity.getResources().getString(R.string.get_cosplay_list_fail);
        }
    }

    private class GetFameListSubscriber extends BaseSubscriber<FameListEntity> {

        public GetFameListSubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public void onNext(FameListEntity entity) {
            super.onNext(entity);
            dataListView.getFameListSuccess(fameMapper.transfer(entity));
        }

        @Override
        public void onCompleted() {
            super.onCompleted();
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            dataListView.getFameListFail();
        }

        @Override
        public String addErrorMessage() {
            return activity.getResources().getString(R.string.get_fame_list_fail);
        }
    }

    private class CollectCosplaySubscriber extends BaseSubscriber<Boolean> {

        public CollectCosplaySubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public String addErrorMessage() {
            return "";
        }

    }

    private class PraiseCosplaySubscriber extends BaseSubscriber<Boolean> {

        public PraiseCosplaySubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public String addErrorMessage() {
            return "";
        }
    }
}
